package cn.tede.file;

import java.io.*;
import java.util.Arrays;

/*本类用来测试字符流的读取*/
public class TestIn2 {
    public static void main(String[] args) {
        method();
        method2();
        //read重载方式测试
        method3();
    }
    /*read重载方法测试*/
    private static void method3() {
        FileInputStream fileInputStream=null;
        try {
            //创建流对象
            fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop");
            //定义一个byte数组，定义每次传输字节数
            byte b[]=new byte[1000];
            int a=0;//b[]数组中有多少元素
           while ((a=fileInputStream.read(b,0, b.length))!=-1){
               System.out.println(a);
               /*获取b数组中的元素*/
               for (int i = 0; i < a; i++) {
                   System.out.print((char) b[i]);
               }
           }
        } catch (IOException e) {
            System.out.println("文件位置找不到");
        }finally {
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void method2() {
        Reader reader=null;
        try {
            //创建流对象
            reader=new BufferedReader(new FileReader("G:\\Project01\\试题\\1.txt"));
            //利用对象读取流操作
            int b=0;
            while ((b=reader.read())!=-1){
                System.out.println((char) b);
            }
        } catch (Exception e) {
            System.out.println("文件位置找不到");
        }finally {
            //关闭流操作
            if (reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void method() {
        Reader reader=null;
        try {
            //创建流对象
            reader=new FileReader("G:\\Project01\\试题\\1.txt");

            int b=0;
            //适用对象，数据读取过程
            while ((b=reader.read())!=-1){
                System.out.println((char) b);
            }
        } catch (IOException e) {
            System.out.println("文件位置不存在");
        }finally {
            //关闭流
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
